//
//  KZUnfamiliarVocabularyImproveVC_version2.m
//  听说弱点强化训练
//
//  Created by 朱凯 on 2018/10/19.
//  Copyright © 2018年 盛超. All rights reserved.
//

#import "KZUnfamiliarVocabularyImproveVC_version2.h"

// vc
#import "KZVocabularyImproveMakeTestVC_version2.h"
#import "KZUserCenterVC.h"
#import "LSWrongQuesController.h"

// view
#import "KZWrongCountView.h"        // 显示错题数量
#import "KZWaveView.h"
#import "KZShadowButton.h"
#import "KZRandomSphereView.h"
#import "KZTurmpetView.h"
// api
#import "KZVocabularyImproveNetApi.h"
#import "ZBFtpTools.h"
#import "LSWrongQuesListManager.h"


@interface KZUnfamiliarVocabularyImproveVC_version2 ()

@property (nonatomic, strong) KZWrongCountView *wrongView;
@property (nonatomic, strong) KZWaveView *waveView;
@property (nonatomic, strong) KZRandomSphereView *sphereView;
@property (nonatomic, strong) UILabel *masterLabel;

@property (nonatomic, strong) KZShadowButton *beginWrongButton;
@property (nonatomic, strong) KZShadowButton *beginButton;


@property (nonatomic, strong) KZVocabularyPackageModel *packageModel;
@property (nonatomic, strong) LSModelWrongQuesList *model;


@end

@implementation KZUnfamiliarVocabularyImproveVC_version2

- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"1");
    self.view.layer.contentsGravity = kCAGravityResizeAspectFill;
    self.view.layer.contents = (__bridge id)[UIImage imageNamed:@"kz-错题生词背景"].CGImage;


    [self setupNaviUI];
    [self setupUI];
    
     [self.navigationController zk_registerBackInteractive:nil gestureConfig:nil];
}

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    [self setNaviAttributeWithTranslucent:true stateBarLightContent:true];

    [self getUnfamiliarVocabularyPackage];
    [self getWrongQuesListInfo];
    
    [_waveView startWave];
    [_wrongView addAnimation];
}
- (void)viewWillDisappear:(BOOL)animated {
    
    [super viewWillDisappear:animated];
    
    [_waveView stopWave];
    [_wrongView removeAnimation];
    [_sphereView removeAnimation];
}

#pragma mark - 视图
- (void)setupNaviUI {
    
    NSString *text = @"错题生词强化训练";
    
    UIControl *backView = [[UIControl alloc] initWithFrame:CGRectMake(0, 0, 200, 40)];
    [backView addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backView];
    
    UIImageView *backImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
//    backImageView.image = [UIImage imageNamed:@"kz-关闭"];
    backImageView.image = [UIImage imageNamed:@"LS-nav_back"];
    [backView addSubview:backImageView];
    [backImageView sizeToFit];
    backImageView.centerY = backView.height / 2;
    
//    UIImageView *iconView = [[UIImageView alloc] initWithFrame:CGRectMake(backImageView.right+5, 10, 20, 20)];
//    iconView.image = [UIImage imageNamed:@"kz-左上角错题生词图标"];
//    [backView addSubview:iconView];
//    [iconView sizeToFit];
//    iconView.centerY = backView.height / 2;
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(backImageView.right+5, 0, [text getSizeOfBlodString:20].width, 40)];
    label.adjustsFontSizeToFitWidth = true;
    label.numberOfLines = 1;
    label.text = text;
    label.font = B_FONT(20);
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor = [UIColor whiteColor];
    [backView addSubview:label];
    
    backView.width = label.right;
    
    UIButton *noteButton = [UIButton buttonWithType:UIButtonTypeCustom];
    noteButton.frame = CGRectMake(0, 0, 17, 17);
    [noteButton setImage:[UIImage imageNamed:@"kz-问号"] forState:UIControlStateNormal];
    [noteButton addTarget:self action:@selector(note) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:noteButton];
}
- (void)setupUI {
    
    _waveView = [[KZWaveView alloc] initWithFrame:self.view.bounds];
    [_waveView setPercent:0.55 animation:false];
    [self.view addSubview:_waveView];
    [_waveView startWave];
    
    // 错题
    UIView *wrongView = [UIView new];
    [self.view addSubview:wrongView];
    [wrongView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(kNavgationBar);
        make.left.right.equalTo(@0);
        make.bottom.equalTo(self.view.mas_centerY).multipliedBy(0.9);
    }];
    _wrongView = [KZWrongCountView new];
    [wrongView addSubview:_wrongView];
    [_wrongView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(W(100));
        make.centerX.equalTo(self.view);
        make.top.mas_equalTo(H(30));
    }];
    
    _beginWrongButton = [[KZShadowButton alloc] init:^(UIButton *button) {
        [button setTitle:@"进入错题重做" forState:UIControlStateNormal];
        button.titleLabel.font = FONT(W(15));
    }];
    _beginWrongButton.colors = @[(__bridge id)kzRGBA(253, 141, 85, 1).CGColor, (__bridge id)kzRGBA(251, 182, 141, 1).CGColor];
    [_beginWrongButton addTarget:self action:@selector(beginWrong) forControlEvents:UIControlEventTouchUpInside];
    [wrongView addSubview:_beginWrongButton];
    [_beginWrongButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(wrongView);
        make.width.equalTo(wrongView).multipliedBy(0.5);
        make.height.mas_equalTo(H(40));
        make.top.equalTo(_wrongView.mas_bottom).offset(H(30));
    }];
    
    
    // 生词
    UIView *unfamiliarView = [UIView new];
    [self.view addSubview:unfamiliarView];
    [unfamiliarView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(wrongView.mas_bottom);
        make.left.right.equalTo(@0);
        if (@available(ios 11.0,*)) {
            make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom);
        } else {
            make.bottom.equalTo(@0);
        }
    }];
    
    // 按钮
    _beginButton = [[KZShadowButton alloc] init:^(UIButton *button) {
        button.backgroundColor = KZThemeColor;
        [button setTitle:@"进入生词听说训练" forState:UIControlStateNormal];
        button.titleLabel.font = FONT(W(15));
    }];
    [_beginButton addTarget:self action:@selector(beginUnfamiliar) forControlEvents:UIControlEventTouchUpInside];
    [unfamiliarView addSubview:_beginButton];
    [_beginButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.width.equalTo(self.view).multipliedBy(0.5);
        make.height.mas_equalTo(H(40));
        make.bottom.mas_equalTo(-H(20));
    }];
    
    // 标注
    _masterLabel = [[UILabel alloc] init];
    _masterLabel.font = FONT(12);
    _masterLabel.textAlignment = NSTextAlignmentCenter;
    _masterLabel.textColor = [UIColor grayColor];
    [unfamiliarView addSubview:_masterLabel];
    [_masterLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(@0);
        make.bottom.equalTo(_beginButton.mas_top).offset(H(-20));
    }];
    
    _sphereView = [KZRandomSphereView new];
    [unfamiliarView addSubview:_sphereView];
    [_sphereView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(@0);
        if (iPad || iPadMini) {
            make.width.equalTo(unfamiliarView).multipliedBy(0.6);
        } else {
            make.width.equalTo(unfamiliarView).multipliedBy(0.75);
        }
        
        make.top.equalTo(@0);
        make.bottom.equalTo(_masterLabel.mas_top);
    }];
    
}

#pragma mark - Net
// 获取听说词汇包信息
- (void)getUnfamiliarVocabularyPackage {
    
    [MBProgressHUD showLoadind:@"正在查询生词包，请稍候..."];
    
    KZVocabularyImproveNetApi *api = [KZVocabularyImproveNetApi new];
    api.requestParam = @{@"stageNo":KGetUserDefaults(@"TestLayer"),
                         @"schoolId":DR_UserInfo.SchoolID,
                         @"stuId":DR_UserInfo.UserID,};
    [api getUnfamiliarVocabularyPackage:^(KZVocabularyPackageModel *model) {
        
        [MBProgressHUD hideHud];
        
        _packageModel = model;
        
        if (_packageModel.currentPackage.Detail.count >= 16) {
            _sphereView.vocabularys = [_packageModel.currentPackage.Detail subarrayWithRange:NSMakeRange(0, 16)];
        } else {
            _sphereView.vocabularys = _packageModel.currentPackage.Detail;
        }
        

        
        // 同时为0，按钮不能点，且隐藏masterlabel
        if (model.PoorMasterVocabularyNum.intValue == 0 &&
            model.BetterMasterVocabularyNum.intValue == 0) {
            _beginButton.colors = @[(__bridge id)UIColor.lightGrayColor.CGColor, (__bridge id)UIColor.lightGrayColor.CGColor];
            _beginButton.enabled = false;
            _masterLabel.hidden = true;
        } else {
            if (model.PoorMasterVocabularyNum.intValue == 0) {
               _beginButton.colors = @[(__bridge id)UIColor.lightGrayColor.CGColor, (__bridge id)UIColor.lightGrayColor.CGColor];
                _beginButton.enabled = false;
            } else {
                _beginButton.colors = nil;
                _beginButton.enabled = true;
            }
            
            _masterLabel.hidden = false;
            
            NSString *text = [NSString stringWithFormat:@"掌握较好%@个，较弱%@个", model.BetterMasterVocabularyNum, model.PoorMasterVocabularyNum];
            NSString *better = [NSString stringWithFormat:@"%@", model.BetterMasterVocabularyNum];
            NSString *poor = [NSString stringWithFormat:@"%@", model.PoorMasterVocabularyNum];
            NSMutableAttributedString *attrText = [[NSMutableAttributedString alloc] initWithString:text];
            [attrText addAttribute:NSForegroundColorAttributeName value:UIColor.redColor range:[text rangeOfString:better]];
            [attrText addAttribute:NSForegroundColorAttributeName value:UIColor.redColor range:[text rangeOfString:poor]];
            _masterLabel.attributedText = attrText.copy;
        }
        
        [self.view viewWithTag:222].hidden = _masterLabel.hidden;
        [_beginButton setNeedsLayout];
        [_beginButton layoutIfNeeded];
        
    } failuer:^(NSString *error) {
        [MBProgressHUD showFailureWithText:error];
    }];
}

- (void)getWrongQuesListInfo {
    LSWrongQuesListManager *manager = [[LSWrongQuesListManager alloc] init];
    NSDictionary *dict = @{@"StuID":DR_UserInfo.UserID,
                           @"SchoolID":KGetUserDefaults(@"SchoolID"),
                           @"StageNo":KGetUserDefaults(@"TestLayer"),
                           @"TypeID":@"-1"
                           };
    manager.requestParam = @{@"DictString":[NSObject objectToJson:dict]};
    WeakObj(self);
    [manager requestWithCompltionSuccess:^(LSModelWrongQuesList *model) {

        selfWeak.model = model;
        if (model.ItemInfoList.count!=0) {
            NSString *ftpServerAddress = _model.FtpServerAddress;
            NSString *ftpUserPwd = _model.sUserPwd;
            NSString *ftpUser = _model.sUserName;
            [ZBFtpTools ZBFtp_setupInitWithHost:ftpServerAddress
                                           user:ftpUser
                                            psw:ftpUserPwd];
            selfWeak.beginWrongButton.colors = @[(__bridge id)kzRGBA(253, 141, 85, 1).CGColor, (__bridge id)kzRGBA(251, 182, 141, 1).CGColor];
            selfWeak.beginWrongButton.enabled = true;
        } else {
            selfWeak.beginWrongButton.colors = @[(__bridge id)UIColor.lightGrayColor.CGColor, (__bridge id)UIColor.lightGrayColor.CGColor];

            selfWeak.beginWrongButton.enabled = false;
        }
        selfWeak.wrongView.count = model.ItemInfoList.count;

    } failuer:^(NSString *error) {

    }];
}

#pragma mark - 事件
- (void)back {
    
    [self dismissViewControllerAnimated:true completion:nil];
}
// 注释
- (void)note {
    
}
// 错题重做
- (void)beginWrong {
    LSWrongQuesController *vc = [[LSWrongQuesController alloc] init];
    vc.model = self.model;
    [self push:vc];
}
// 生词训练
- (void)beginUnfamiliar {
    
    if (!_packageModel) {
        return;
    }
    if (_packageModel.currentPackage.Detail.count == 0) {
        [MBProgressHUD showFailureWithText:@"没有需要训练的生词了！"];
        return;
    }
    
    KZVocabularyImproveMakeTestVC_version2 *vc = [KZVocabularyImproveMakeTestVC_version2 new];
    vc.type = KZVocabularyTypeUnfamiliar;
    vc.packageModel = _packageModel;
    [self.navigationController pushViewController:vc animated:true];

}
@end
